package com.oa.controller;

import com.oa.pojo.Result;
import com.oa.pojo.User;
import com.oa.service.MenuService;
import com.oa.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 吴世荣
 * @description 针对表【user】接口实现
 * @createDate 2023-10-22
 */

@Api(tags = "用户：在线接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;


    @CrossOrigin
    @ApiOperation(value="用户登录",httpMethod = "POST",notes = "返回包含用户类的result类型json格式")
    @PostMapping("/login")
    public Result login(String usernameOrPhone,String password ){
        System.out.println("执行登陆操作");
        User user = new User();
        user=userService.login(usernameOrPhone,password);
        if(user!=null){
            return Result.success(user);
        }else{
            return Result.error("信息有误，登录失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value="根据用户条件查询(id，username,gender(1/2/3),married(1/2))",httpMethod = "GET",notes = "返回包含用户类的result类型json格式")
    @GetMapping("/selectAll")
    public Result selectAll(Integer id,String username,Integer gender,Integer married) {
        System.out.println("执行多条件查询");

        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setGender(gender);
        user.setMarried(married);

        System.out.println(user.getAddress());
        List<User> userList=new ArrayList<>();
        userList=userService.selectAll(user);
        if(userList!=null){
            return Result.success(userList);
        } else {
            return Result.error("查询结果为空");
        }

    }

    @CrossOrigin
    @ApiOperation(value="用户信息更改",httpMethod = "POST",notes = "返回的result类型json格式")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        System.out.println("执行更改用户信息");
        int flag=userService.updateUser(user);
        if(flag==1){
            return Result.success();
        }else if (flag==-1){
            return Result.error("-1");
        }else if (flag==-2){
            return Result.error("-2");
        }else if (flag==-3){
            return Result.error("-3");
        }else {
            return Result.error("0");
        }
    }

    @CrossOrigin
    @ApiOperation(value="添加用户",httpMethod = "POST",notes = "返回的result类型json格式")
    @PostMapping("/insertUser")
    public Result insertUser(@RequestBody User user){
        System.out.println("执行添加用户信息");
        int flag=userService.insertUser(user);
        if(flag==1){
            return Result.success();
        }else if (flag==-1){
            return Result.error("失败");
        }else {
            return Result.error("0");
        }
    }

    @CrossOrigin
    @ApiOperation(value="删除用户",httpMethod = "GET",notes = "返回的result类型json格式")
    @GetMapping("/deleteUser")
    public  Result deleteUser(Integer id){
        userService.deleteUser(id);
        return Result.success();
    }
}
